1828C - Counting Orders - CodeForces Solution


combinatorics sortings

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
#include <set>
#include <cstring>
#include <map>
#include <cmath>

using namespace std;
typedef long long ll;
#define endl "\n";

ll G[200005];
const ll mod = 1000000007;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (int i=0; i<n; i++) {
        cin >> a[i];
    }
    for (int i=0; i<n; i++) {
        cin >> b[i];
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    
    if (a[0] <= b[0]) {
        cout << 0 << endl;
        return;
    }

    G[0] = n;
    ll less = 0;
    for (int i=1; i<n; i++) {
        ll diff = 0;
        while (less < n && a[less] <= b[i]) {
            less++;
            diff++;
        }
        G[i] = G[i-1] - diff;
        if (G[i] <= 0) {
            cout << 0 << endl;
            return;
        }
    }

    ll ans = 1, df = 0;
    for (int i=n-1; i>=0; i--) {
        if (G[i] - df <= 0) {
            cout << 0 << endl;
            return;
        }
        ans *= (G[i] - df) % mod;
        ans %= mod;
        df++;
    }
    cout << ans % mod << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--){ 
        solve();
        memset(G, 0, sizeof(G));
    }
}


Comments

Submit
0 Comments
More Questions

1303D - Fill The Bag
1198B - Welfare State
1739B - Array Recovery
1739C - Card Game
1739A - Immobile Knight
1739D - Reset K Edges
817B - Makes And The Product
1452C - Two Brackets
400B - Inna and New Matrix of Candies
870B - Maximum of Maximums of Minimums
1600E - Array Game
1149A - Prefix Sum Primes
277A - Learning Languages
769A - Year of University Entrance
1738A - Glory Addicts
1738C - Even Number Addicts
1064B - Equations of Mathematical Magic
384A - Coder
1738B - Prefix Sum Addicts
1352D - Alice Bob and Candies
1316D - Nash Matrix
1548B - Integers Have Friends
348A - Mafia
315B - Sereja and Array
204A - Little Elephant and Interval
385B - Bear and Strings
114C - Grammar Lessons
1427A - Avoiding Zero
583A - Asphalting Roads
1358B - Maria Breaks the Self-isolation